package com.soda.servlet;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.soda.bean.Industry;
import com.soda.bean.Information;
import com.soda.bean.News;
import com.soda.bean.Notice;
import com.soda.service.IndustryService;
import com.soda.service.InformationService;
import com.soda.service.NewsService;
import com.soda.service.NoticeService;
import com.soda.service.impl.IndustryServiceImpl;
import com.soda.service.impl.InformationServiceImpl;
import com.soda.service.impl.NewsServiceImpl;
import com.soda.service.impl.NoticeServiceImpl;
import com.soda.util.IndustryExcel;
import com.soda.util.InformationExcel;
import com.soda.util.NewsExcel;
import com.soda.util.NoticeExcel;

public class TestServlet extends HttpServlet {

	NewsService ns = new NewsServiceImpl();
	NoticeService not=new NoticeServiceImpl();
	InformationService is=new InformationServiceImpl();
	IndustryService ids=new IndustryServiceImpl();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String param = request.getParameter("param");// 获得参数，由参数判断执行方法
		if("news".equals(param)){
			news(request,response);
			return;
		}
		if("notice".equals(param)){
			notice(request,response);
			return;
		}
		if("Information".equals(param)){
			Inform(request,response);
			return;
		}
		if("Industry".equals(param)){
			industry(request,response);
			return;
		}
		// 获得参数，由参数判断执行方法
 
	}
 
	protected void news(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			// 获取要导出的数据存入List
			String delid = request.getParameter("newsid");
			String[] count = delid.split(",");
			//
			// 获取新闻
			List<News> news = ns.exportNews(count);
			// 导出
			NewsExcel excel = new NewsExcel();
			HSSFWorkbook wb = excel.export(news, "新闻表");
			// 定义客户端下载得到的文件名
			String filename = "news.xls";
			// 设置响应的类型 为 文件下载类型
			response.setContentType("application/x-msdownload");
			// 设置 下载弹出框 中 显示文件名
			URLEncoder.encode(filename, "utf-8");
			// 设置响应头文件
			response.setHeader("Content-Disposition", "attachment;filename=\""
					+ filename + "\"");
			// 获得响应中的输出字节流
			ServletOutputStream os = response.getOutputStream();
			wb.write(os);

			os.flush();

			os.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	protected void industry(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			// 获取要导出的数据存入List
			String delid = request.getParameter("indid");
			String[] count = delid.split(",");
			//
			// 获取新闻
			List<Industry> list = ids.exportIndustry(count);
			// 导出
			IndustryExcel excel = new IndustryExcel();
			HSSFWorkbook wb = excel.export(list, "新闻表");
			// 定义客户端下载得到的文件名
			String filename = "industry.xls";
			// 设置响应的类型 为 文件下载类型
			response.setContentType("application/x-msdownload");
			// 设置 下载弹出框 中 显示文件名
			URLEncoder.encode(filename, "utf-8");
			// 设置响应头文件
			response.setHeader("Content-Disposition", "attachment;filename=\""
					+ filename + "\"");
			// 获得响应中的输出字节流
			ServletOutputStream os = response.getOutputStream();
			wb.write(os);

			os.flush();

			os.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	//公告
	protected void notice(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 
			try {
				// 获取要导出的数据存入List
				String delid = request.getParameter("noticeid");
				String[] count = delid.split(",");
				//
				// 获取新闻
				List<Notice> notice = not.exportNotice(count);
				// 导出
				NoticeExcel excel = new NoticeExcel();
				HSSFWorkbook wb = excel.export(notice, "新闻表");
				// 定义客户端下载得到的文件名
				String filename = "notice.xls";
				// 设置响应的类型 为 文件下载类型
				response.setContentType("application/x-msdownload");
				// 设置 下载弹出框 中 显示文件名
				URLEncoder.encode(filename, "utf-8");
				// 设置响应头文件
				response.setHeader("Content-Disposition", "attachment;filename=\""
						+ filename + "\"");
				// 获得响应中的输出字节流
				ServletOutputStream os = response.getOutputStream();
				wb.write(os);

				os.flush();

				os.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	//咨询
	protected void Inform(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			// 获取要导出的数据存入List
			String delid = request.getParameter("inid");
			String[] count = delid.split(",");
			//
			// 获取新闻
			List<Information> news = is.exportInform(count);
			// 导出
			InformationExcel excel = new InformationExcel();
			HSSFWorkbook wb = excel.export(news, "新闻表");
			// 定义客户端下载得到的文件名
			String filename = "news.xls";
			// 设置响应的类型 为 文件下载类型
			response.setContentType("application/x-msdownload");
			// 设置 下载弹出框 中 显示文件名
			URLEncoder.encode(filename, "utf-8");
			// 设置响应头文件
			response.setHeader("Content-Disposition", "attachment;filename=\""
					+ filename + "\"");
			// 获得响应中的输出字节流
			ServletOutputStream os = response.getOutputStream();
			wb.write(os);

			os.flush();

			os.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
	}
	
	
}
